/**
 * 
 */
package com.yidu.drug.action;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yidu.drug.common.Page;
import com.yidu.drug.model.MemberModel;
import com.yidu.drug.service.MemberService;

/**
 * 会员管理的action
 * @author 易玉凤
 * @date 2018年8月13日下午2:13:11
 * @version 1.0
 */
@Controller
@RequestMapping("/member")
public class MemberAaction {
	@Resource
	private MemberService memberService;
	
	/**
	 * 根据条件查询所有
	 * @param page
	 * @param limit
	 * @param memberName
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping("/findAll")
	@ResponseBody
	public Map<String, Object> findAll(String page,String limit,String memberName) throws UnsupportedEncodingException{
		Page pages = new Page();
		pages.setPage(Integer.valueOf(page));
		pages.setRows(Integer.valueOf(limit));
		
		MemberModel MemberModel = new MemberModel();
		MemberModel.setMemberName(memberName);
		MemberModel.setPage(pages);
		
		List<MemberModel> list = memberService.findAll(MemberModel);
		List<MemberModel> lists= new ArrayList<MemberModel>(); 
		for(MemberModel meb:list){
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			meb.setRegistDates(sdf.format(meb.getRegistDate()));
			lists.add(meb);
		}
		//查询出的信息的总行数
		Long count = memberService.findRows(MemberModel);
		//加入MAP集合
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code", 0);
		map.put("msg", "");
		map.put("count", count);
		map.put("data", lists);
		return map;	
	}
	
	/**
	 * 查询所有
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/findAllMember")
	@ResponseBody
	public List<MemberModel> findAllMember(HttpServletRequest request,HttpServletResponse response) throws IOException{
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		List<MemberModel> list = memberService.findAllMember();
		return list;
	}
	
	/**
	 * 增加会员
	 * @param memberModel
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping("/add")
	public void add(MemberModel memberModel) throws UnsupportedEncodingException{
		
		String memberId = UUID.randomUUID().toString();
		memberModel.setMemberId(memberId);
		
		memberService.save(memberModel);
	}
	
	/**
	 * 删除
	 * @param memberId
	 * @return
	 */
	@RequestMapping("/updateById")
	@ResponseBody
	/*public Message updateById(MemberModel member) {
		memberService.updateById(member);
		Message msg = new Message(1,"删除成功！");
		return msg;
	}*/
	public String updateById(String memberId){
		System.out.println("进入到updateById方法");
		int rows = memberService.updateById(memberId);
		if(rows>0){
			return "ok";
		}else{
			return "no";
		}
		
	}
	
	/**
	 * 修改页面
	 * @param request
	 * @param response
	 * @param session
	 * @param memberId
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/showUpdate")
	@ResponseBody
	public MemberModel showUpdate(HttpServletRequest request,HttpServletResponse response,HttpSession session,String memberId) throws IOException{
		MemberModel memberModel = memberService.findById(memberId);
		session.setAttribute("memberId", memberId);
		System.out.println("会员等级="+memberModel.getGradeName());
		return memberModel;
	}
	
	/**
	 * 修改
	 * @param memberModel
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping("/update")
	public void update(MemberModel memberModel) throws UnsupportedEncodingException{
		
		
		memberService.update(memberModel);
		
	}
}
